<?php

namespace App\Game\Logic;

use App\Game\Conf\GameProgress;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\AStrategy;
use App\Game\Core\Message;
use App\Game\Core\Packet;
use App\Game\Core\Robot;
use App\Game\Timer\GameTimer;
use App\Task\GameSyncTask;
use App\Task\GameTimerTask;

/**
 *  定缺响应
 */
class ChooseColor extends AStrategy
{
    /**
     * 执行方法
     */
    public function exec()
    {
        $game_conf = config('game');
        $account = $this->_params['userinfo']['account'];
        $user_room_map_key = sprintf($game_conf['user_room'], $account);
        $room_no = redis()->get($user_room_map_key);
        $color = isset($this->_params['data']['color']) ?
            $this->_params['data']['color'] : rand(1, 3);
        if (!checkGameProgress($room_no, GameProgress::GAME_CHOOSE_COLOR)) {
            $msg = array('status' => 1, 'info' => '定缺非法, 还未到定缺阶段！');
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP, 0);
            return;
        }

        //检查操作是否已经超时
        if (!checkCounter($room_no)) {
            $msg = ['status' => 1, 'info' => '该操作已超时，系统已自动为您执行默认操作'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP, 0);
            return;
        }

        if (empty($room_no)) {
            $msg = ['status' => 1, 'info' => '房间号不存在'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP, 0);
            return;
        }
        if (empty($color)) {
            $msg = ['status' => 2, 'info' => '请选择要定缺的花色'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP, 0);
            return;
        }
        if (!in_array($color, [1, 2, 3])) {
            $msg = ['status' => 3, 'info' => '请选择正确的定缺的花色'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP, 0);
            return;
        }
        $chair_id = findChairId($room_no, $account); // 座位号

//        $msg = ['status' => 0, 'chair_id' => $chair_id, 'color' => $color];
//        Message::broadcast($room_no, $msg, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP);


        $room_color_key = sprintf($game_conf['user_room_color'], $room_no);
        redis()->hSet($room_color_key, (string)$chair_id, $color);

        $robots = Robot::getRoomRobot($room_no);
        $robotNum = count($robots);
        $realNum = 4 - $robotNum;


//        $ptype = getGameProgressType($room_no);
//        if ($ptype) { //单机
//            ddTitle('do_choose_color 1111111  ');
//            //清除前一个定时器
//            putTimerDel($room_no);
//            GameTimer::do_choose_color($room_no);
//            return;
//        }


        $arr = redis()->hGetAll($room_color_key);
        if (!$arr) {
            $arr = [];
        }
        dd('arr', $arr);
        if (count($arr) >= $realNum) {
            $colors = [];
            foreach ($arr as $cid => $color) {
                $colors[$cid] = $color;
            }
            redis()->del($room_color_key);
            redis()->set($room_color_key, json_encode($colors));
            dd('colors', $colors);
            $index = 0;
            foreach ($arr as $k => $v) {
                $index++;
                if ($index == $realNum) {
                    ddNode($index);
                    if ($k == $chair_id) {
                        ddTitle('do_choose_color 0000000000  ');
                        //清除前一个定时器
                        putTimerDel($room_no);
                        GameTimer::do_choose_color($room_no, $colors);
                    }
                }
            }
        }
    }


    protected function resp($msg)
    {
        $fd = $this->_params['userinfo']['fd'];
        $serv = $this->_params['serv'];
        $data = Packet::packFormat('ERROR', 0, $msg);
        $return_data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::SUB_GAME_CHOOSE_COLOR_RESP);
        $serv->push($fd, $return_data, WEBSOCKET_OPCODE_BINARY);
    }

}